package org.example.course.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.course.common.Constast;
import org.example.course.common.DataGridView;
import org.example.course.common.XmSelect;
import org.example.course.domain.*;
import org.example.course.dto.*;
import org.example.course.mapper.CheckevaluationMapper;
import org.example.course.mapper.CourseMapper;
import org.example.course.mapper.GraduationMapper;
import org.example.course.mapper.StandardMapper;
import org.example.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CourseServiceImpl
 * @Description 课程
 * @Date 2020/5/8 15:55
 * @Created by
 */
@Service
@Transactional
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper mapper;

    @Autowired
    private CheckevaluationMapper checkevaluationMapper;

    @Autowired
    private StandardMapper standardMapper;

    @Autowired
    private GraduationMapper graduationMapper;

    @Override
    public DataGridView findPage(CourseModel courseModel) {
        //分页插件 PageHelper
        PageHelper.startPage(courseModel.getPage(), courseModel.getLimit());
        List<Course> list = mapper.findAll(courseModel);
        //PageHelper 获取分页信息
        PageInfo<Course> pageInfo = new PageInfo(list);
        return new DataGridView(pageInfo.getTotal(), list);
    }

    @Override
    public boolean create(Course course) {
        int msg = mapper.insert(course);
        return msg > 0;
    }

    @Override
    public boolean update(Course course) {
        int msg = mapper.updateByPrimaryKeySelective(course);
        return msg > 0;
    }

    @Override
    public boolean delete(String id) {
        int msg = mapper.deleteByPrimaryKey(Integer.valueOf(id));
        return msg > 0;
    }

    @Override
    public DataGridView findGraduation(String id) {
        List<Graduation> graduationList = mapper.findGraduation(id);
        return new DataGridView(graduationList);
    }

    @Override
    public boolean deleteGraduation(String graduationId, String courseId) {
        int msg = mapper.deleteGraduation(Integer.valueOf(graduationId), Integer.valueOf(courseId));
        return msg > 0;
    }

    @Override
    public DataGridView graduationSelect(String id) {
        //选查询已选择的毕业要求ID
        List<Integer> ids = mapper.findGraduationId(id);
        //查询未选中的数据 not in
        List<Graduation> graduations = mapper.graduation(ids);



        List<XmSelect> graduationList = new ArrayList<>();
        for (Graduation graduation : graduations) {
            XmSelect xmSelect = new XmSelect();
            xmSelect.setName(graduation.getContent() + ":" + graduation.getPower());
            xmSelect.setValue(graduation.getId());
            graduationList.add(xmSelect);

        }

//        List<XmSelect> graduationList = mapper.graduationSelect(ids);

        return new DataGridView(graduationList);
    }

    @Override
    public boolean addSelectArr(Integer[] ids, String courseId) {
        int msg = mapper.addSelectArr(ids, Integer.valueOf(courseId));
        return msg > 0;
    }

    @Override
    public DataGridView findDetails(String id) {
        List<GraduationModel> graduationList = mapper.findDetails(id);
        return new DataGridView(graduationList);
    }

    @Override
    public DataGridView findCheck(String id) {
        List<Check> graduationList = mapper.findCheck(Integer.valueOf(id));
        return new DataGridView(graduationList);
    }

    @Override
    public DataGridView findDetailsCheck(String id) {
        List<CheckModel> graduationList = mapper.findDetailsCheck(Integer.valueOf(id));
        return new DataGridView(graduationList);
    }

    @Override
    public DataGridView findStandard(String id) {


        //更具课程ID 查询考核环节
        List<Check> checks = mapper.findCheck(Integer.valueOf(id));

        List<List<StandardModel>> allList = new ArrayList<>();
        //查询是否有记录
        for (Check check : checks) {
            //更具考核环节ID 查询是否有标准记录
            List<StandardModel> standardModels = standardMapper.findStandByCheckId(check.getId());
            if (standardModels != null && standardModels.size() > 0) {
                allList.add(standardModels);
            }

        }
        if (allList != null && allList.size() > 0) {
            //数据库存在数据 直接返回
            return new DataGridView(allList);
        }

        //数据库没有数据   组装数据返回
        for (int i = 0; i < checks.size(); i++) {
            List<StandardModel> list = new ArrayList<>();
            String check = "check";
            int j = i + 1;
            check += j;
            List<Checkevaluation> checkevaluations = checkevaluationMapper.findAllByCheck(Integer.valueOf(id), check);
            for (Checkevaluation cv : checkevaluations) {

                String inOutline = null;
                inOutline = getString(j, cv, inOutline);

                StandardModel standardModel = new StandardModel();
                standardModel.setCourseId(Integer.valueOf(id));
                standardModel.setCheckId(checks.get(i).getId());
                standardModel.setName(checks.get(i).getName());

                standardModel.setTarget(cv.getTarget());
                standardModel.setCheckevaluationId(cv.getId());

                standardModel.setInOutline(inOutline);
                String average = checks.get(i).getAverage();
                standardModel.setCheckDegree(average);


                double avg = Double.valueOf(average).doubleValue();
                double in = Double.valueOf(inOutline).doubleValue();
                standardModel.setCheckAverage(String.valueOf(avg * (in / 100)));


                list.add(standardModel);
            }
            allList.add(list);

            //自动生成数据到数据库
            standardMapper.batchInsert(list);
        }

        return new DataGridView(allList);

    }

    private String getString(int j, Checkevaluation cv, String inOutline) {
        switch (j) {
            case 1:
                inOutline = cv.getCheck1();
                break;
            case 2:
                inOutline = cv.getCheck2();
                break;
            case 3:
                inOutline = cv.getCheck3();
                break;
            case 4:
                inOutline = cv.getCheck4();
                break;
            case 5:
                inOutline = cv.getCheck5();
                break;
            default:
                break;
        }
        return inOutline;
    }

    @Override
    public DataGridView findAnalyze(String id) {

        //更具课程ID 查询考核环节
        List<Check> checks = mapper.findCheck(Integer.valueOf(id));

        List<List<StandardModel>> allList = new ArrayList<>();
        //查询是否有记录
        for (Check check : checks) {
            //更具考核环节ID 查询是否有标准记录
            List<StandardModel> standardModels = standardMapper.findStandByCheckId(check.getId());
            if (standardModels != null && standardModels.size() > 0) {
                for (StandardModel standardModel : standardModels) {
                    standardModel.setCheckDegree(standardModel.getCheckDegree() + "%");
                }
                allList.add(standardModels);
            }

        }
        if (allList != null && allList.size() > 0) {
            //数据库存在数据 直接返回
            return new DataGridView(allList);
        }
        return new DataGridView(Constast.ERROR, Constast.NO_DATA);
    }

    @Override
    public DataGridView findConclude(String id) {
        //查询课程名字
        Course course = mapper.selectByPrimaryKey(Integer.valueOf(id));
        String courseName = course.getName();

        List<Conclude> list = new ArrayList<>();

        //更具课程ID查询 毕业要求
        List<Graduation> graduationList = mapper.findGraduation(id);


        //查询毕业要求对应的 考核分数 加和
        for (Graduation g : graduationList) {
            double power = Double.valueOf(g.getPower()).doubleValue();
            Conclude conclude = new Conclude();
            //课程名称
            conclude.setCourseName(courseName);
            //指标点
            conclude.setContent(g.getContent());
            //权重系数
            conclude.setPower(g.getPower());

            //查询每个考核环节的分数
            List<Checkevaluation> checkevaluationList = checkevaluationMapper.findAllByGraduationId(g.getId());
            int sum = 0;
            List<Integer> ids = new ArrayList<>();
            for (Checkevaluation c : checkevaluationList) {
                int results = Integer.valueOf(c.getResults()).intValue();
                sum += results;
                //查询考核环节情况分析表 信息
                ids.add(c.getId());
            }
            //考核总分
            conclude.setPoints(String.valueOf(sum));
            //更具CheckevaluationID集合查询 standard表
            double average = 0;
            if (ids != null && ids.size() > 0) {
                List<Standard> standardList = standardMapper.findByCheckevaluationIds(ids);
                for (Standard standard : standardList) {
                    average += Double.valueOf(standard.getCheckAverage());
                }
            }
            //考核平均分
            conclude.setAverage(String.valueOf(average));
            //达成度计算
            double concl = (average / sum) * power;
            conclude.setConclude(String.valueOf(concl));
            list.add(conclude);
        }

        return new DataGridView(list);
    }

    @Override
    public Course insert(String name) {
        Course course = new Course();
        course.setName(name);
        //根据课程名称查询是否存在
        Course dbCourse = mapper.findByName(name);
        if (null != dbCourse) {
            //返回查询ID
            course.setId(dbCourse.getId());
        } else {
            //返回插入 后生成的ID
            int insert = mapper.insert(course);
        }
        return course;
    }

    @Override
    public Boolean insertGraduation(Integer courseId, List<Graduation> graduations) {
        for (Graduation graduation : graduations) {
            Graduation dbGraduation = graduationMapper.findByPowerAndContent(graduation.getPower(), graduation.getContent());
            //查询数据库是否存在该  毕业要求
            Integer graduationId;
            if (null != dbGraduation) {
                //毕业要求 存在返回ID
                graduationId = dbGraduation.getId();
            } else {
                //毕业要求 不存在  插入
                graduationMapper.insert(graduation);
                //返回ID
                graduationId = graduation.getId();
            }
            //courseId  graduationId 插入关联关系到中间表
            mapper.insertCourseGraduation(courseId, graduationId);
        }
        return true;
    }
}
